<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Project_model extends CI_Model {

    public $tablename = 'hpg_project';

    public function __construct()
    {
        parent::__construct();
    }
    
    public function gePageList($project_name, $type=0, $page=1, $limit=20){
        $start = ($page-1)*$limit;
        $query = $this->db;
        if( $project_name ){
            $query->like('project_name', $project_name);
        }
        if( $type ){
            $query->where('is_start', 1);
        }
        $count = $query->count_all_results($this->tablename, false);
        $list = $query->select('id,project_name,company_name,company_legal,company_regaddr,
            company_url,company_area,company_addr,company_desc,company_industry,project_source,project_mark,project_creator,project_push_status,
            project_first_time,project_revisit_time,project_team,manager,execute_status,finance_rotation,finance_expect,project_cover,finance_history,manager,ctime')->limit($limit, $start)->order_by('id', 'desc')->get_array();
        return [intval($count), $list ? $list : []];
    }
    
    public function insert($data){
        if( !$data ){
            return false;
        }
        $res = $this->db->insert($this->tablename, $data);
        return $this->db->insert_id();
    }
    
    public function update($map, $data){
        if( !$map || !$data ){
            return false;
        }
        $res = $this->db->update($this->tablename, $data, $map);
        return $res;
    }
    
    public function getProjectOne($id, $fields='*'){
        if( !$id ){
            return false;
        }
        $data = $this->db->from($this->tablename)->select($fields)->where('id', $id)->fetchOne();
        return $data;
    }
    
    public function getProjectByIds($ids){
        if( !$ids ){
            return false;
        }
        $data = $this->db->from($this->tablename)->select('*')->where_in('id', $ids)->get_array();
        $temp = [];
        foreach($data as $v){
            $temp[$v['id']] = $v;
        }
        return $temp;
    }
}